package com.yan.gulimall.member.controller;

import com.yan.common.exception.BizCodeEnume;
import com.yan.common.utils.PageUtils;
import com.yan.common.utils.R;
import com.yan.gulimall.member.entity.MemberEntity;
import com.yan.gulimall.member.exception.PhoneExistException;
import com.yan.gulimall.member.exception.UsernameExistException;
import com.yan.gulimall.member.service.MemberService;
import com.yan.gulimall.member.vo.MemberLoginVo;
import com.yan.gulimall.member.vo.MemberRegisterVo;
import com.yan.gulimall.member.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author yan
 * @email yan@gmail.com
 * @date 2020-12-22 14:13:37
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 三方登录功能
     * @return
     */
    @PostMapping("/oauth/login")
    public R oauthLogin(@RequestBody SocialUserVo vo) throws Exception {

        MemberEntity user = memberService.login(vo);
        if (user!=null){
            return R.ok().setData(user);
        } else {
            return R.error(BizCodeEnume.LOGIN_EXCEPTION.getCode(),BizCodeEnume.LOGIN_EXCEPTION.getMsg());
        }
    }

    /**
     * 登录功能
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody MemberLoginVo vo){

        MemberEntity user = memberService.login(vo);
        if (user!=null){
            return R.ok().setData(user);
        } else {
            return R.error(BizCodeEnume.LOGIN_EXCEPTION.getCode(),BizCodeEnume.LOGIN_EXCEPTION.getMsg());
        }
    }

    /**
     * 注册功能
     * @return
     */
    @PostMapping("/register")
    public R reg(@RequestBody MemberRegisterVo vo){

        try {
            memberService.register(vo);
        } catch (PhoneExistException e){
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e){
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
